
#include "stdafx.h"
#include "head.h"

MapData::MapData(char* pcDataPath)
{
	char acFileName[128];
	sprintf(acFileName, "%s\\%s", pcDataPath, DATA_FILE_NAME);
	this->m_fp = fopen(acFileName, "rb");
	assert(m_fp);

	m_pcBuff = (char*)malloc(1024*1024);
	fread(&m_stFileInfo, sizeof(m_stFileInfo), 1, m_fp);
	fread(m_pcBuff, m_stFileInfo.lManageSize, 1, m_fp);

	char* pcTemp = m_pcBuff;
	parseParcelMng(&m_stParcelMng, &pcTemp);

	assert(m_stFileInfo.lManageSize == (pcTemp - m_pcBuff));

}

MapData::~MapData()
{
	free(m_pcBuff);
	fclose(m_fp);
}

bool MapData::RequestData(float x1, float y1, float x2, float y2)
{
	PointF_t pt1,pt2;
	pt1.x = x1;	pt1.y = y1;
	pt2.x = x2;	pt2.y = y2;

	vr.clear();
	er.clear();

	parseParcelData(&m_stParcelMng, pt1, pt2);


	return true;
}

void MapData::parseParcelMng(ParcelMng_t* pstParcelMng, char** ppcData)
{
	int i;
	DataRec_t* pstDataRec = (DataRec_t*)*ppcData;
	*ppcData += sizeof(DataRec_t); 
	pstParcelMng->cDivX = pstDataRec->cDivX;
	pstParcelMng->cDivY = pstDataRec->cDivY;
	pstParcelMng->cFlag = pstDataRec->flag;
	pstParcelMng->stRect = pstDataRec->stRect;
	
	if(pstParcelMng->cFlag == DATA_FLAG_SPLIT)
	{
		for(i=0;i<SPLIT_CNT*SPLIT_CNT;i++)
		{
			ParcelMng_t* pstDivParcelMng = new ParcelMng_t();
			parseParcelMng(pstDivParcelMng, ppcData);
			pstParcelMng->vecParcelMng.push_back(pstDivParcelMng);
		}
	}
	else if(pstParcelMng->cFlag == DATA_FLAG_NO_SPLIT)
	{
		DataOffset_t* pstDataOffset = (DataOffset_t*)*ppcData;
		*ppcData += sizeof(DataOffset_t);
		pstParcelMng->lNodeCnt = pstDataOffset->lNodeCnt;
		pstParcelMng->lNodeOffset = pstDataOffset->lNodeOffset;
		pstParcelMng->lEdgeCnt = pstDataOffset->lEdgeCnt;
		pstParcelMng->lEdgeOffset = pstDataOffset->lEdgeOffset;
		pstParcelMng->lObjectOffset = pstDataOffset->lObjectOffset;
		pstParcelMng->lObjectCnt = pstDataOffset->lObjectCnt;
	}
}

void MapData::parseParcelData(ParcelMng_t* pstParcelMng, PointF_t pt1, PointF_t pt2)
{
	int i;
	RectF_t rect;
	rect.A = pt1;
	rect.B = pt2;
	if(Lib_isOverlapRectAndRect(rect, pstParcelMng->stRect))
	{
		if(pstParcelMng->cFlag == DATA_FLAG_SPLIT)
		{
			for(i=0;i<pstParcelMng->vecParcelMng.size();i++)
			{
				parseParcelData(pstParcelMng->vecParcelMng[i], pt1, pt2);
			}
		}
		else if(pstParcelMng->cFlag == DATA_FLAG_NO_SPLIT)
		{
			char* pcTemp = m_pcBuff;
			fseek(m_fp, sizeof(FileInfo_t) + m_stFileInfo.lManageSize+pstParcelMng->lNodeOffset, SEEK_SET);
			fread(pcTemp, pstParcelMng->lNodeCnt * sizeof(Node_t), 1, m_fp);

			for(i=0;i<pstParcelMng->lNodeCnt;i++)
			{
				Node_t* pstNode = (Node_t*)pcTemp;
				pcTemp += sizeof(Node_t);

				Vertex_Record vertex(pstNode->id, pstNode->pt.x, pstNode->pt.y);
				vr.push_back(vertex);
			}
			
			pcTemp = m_pcBuff;
			fseek(m_fp,sizeof(FileInfo_t) + m_stFileInfo.lManageSize+pstParcelMng->lEdgeOffset, SEEK_SET);
			fread(pcTemp, pstParcelMng->lEdgeCnt * sizeof(Edge_t), 1, m_fp);

			for(i=0;i<pstParcelMng->lEdgeCnt;i++)
			{
				Edge_t* pstEdge = (Edge_t*)pcTemp;
				pcTemp += sizeof(Edge_t);

				Edge_Record edge(pstEdge->id, pstEdge->sid, pstEdge->eid, pstEdge->lenth);
				er.push_back(edge);
			}

			pcTemp = m_pcBuff;
			fseek(m_fp,sizeof(FileInfo_t) + m_stFileInfo.lManageSize+pstParcelMng->lObjectOffset, SEEK_SET);
			fread(pcTemp, pstParcelMng->lObjectCnt * sizeof(Object_t), 1, m_fp);

			for(i=0;i<pstParcelMng->lObjectCnt;i++)
			{
				Object_t* pstObject = (Object_t*)pcTemp;
				pcTemp += sizeof(Object_t);

				Object_Record obj(pstObject->id, pstObject->eid, pstObject->distance);
				or.push_back(obj);
			}

			TRACE("Load Parcel Data (%f,%f) (%f,%f) NodeCnt:%d EdgeCnt:%d ObjectCnt:%d\n", 
				pstParcelMng->stRect.A.x, pstParcelMng->stRect.A.y,
				pstParcelMng->stRect.B.x, pstParcelMng->stRect.B.y,
				pstParcelMng->lNodeCnt, pstParcelMng->lEdgeCnt, pstParcelMng->lObjectCnt);

		}
	}
}